const express = require('express');
const jwt = require('jsonwebtoken');
const {jwtSecret,jwtExpTime,publicKey} = require('../utils/config');
const bodyParser = require('body-parser');
const {sm2} = require('sm-crypto-v2');
const ApiResult = require('../utils/apiResult');

const router = express.Router();
const jsonParser = bodyParser.json();

router.get('/',(req,res)=>{
    let kp = sm2.generateKeyPairHex();
    //const puk = kp.publicKey;
    //console.log(kp.privateKey);
    let en = sm2.doEncrypt("中午测额是admin123",kp.publicKey,1,{asn1:true});
    console.log(en);
    let den = sm2.doDecrypt(en,kp.privateKey,1,{asn1:true});
    console.log(den);
    
    
    res.send('首页');
});
router.get('/genKey',(req,res)=>{
    let kp = sm2.generateKeyPairHex();
    console.log('publicKey:'+kp.publicKey);
    console.log('privateKey:'+kp.privateKey);
    res.send(ApiResult.success(null,'success'));
})
router.get('/getPublicKey',(req,res)=>{
    res.append('c-token','c-001');
    res.json(ApiResult.success(publicKey));
});
router.post('/login',jsonParser,(req,res)=>{
    const token = 'Bearer '+ jwt.sign(
        {
            yhid: req.body.yhid,
            admin: true,
        },
        jwtSecret,
        {
            algorithm: 'HS256',
            expiresIn: jwtExpTime
        }
    );
    res.json(ApiResult.success(token));
});


module.exports = router;